package com.example.demo.controller;

import com.example.demo.entity.LoginForm;
import com.example.demo.entity.Response;
import com.example.demo.service.Impl.LoginImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/account")
@RequiredArgsConstructor
public class LoginController {
    private final LoginImpl loginImpl;
    @PostMapping("/login")
    public ResponseEntity<Response> login(@RequestBody LoginForm loginForm) {
        if (loginImpl.login(loginForm) == null) {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("account",false))
                            .message("登陆失败，请检查账号或密码")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } else {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("account",loginImpl.login(loginForm)))
                            .message("登录成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        }
    }

}
